
import { _decorator, Component, Node, UITransform, Vec2, Vec3 } from 'cc';
import { MonsterOrigin } from './MonsterOrigin';
import { ConfigKey, configKey, EventName, monster_base_config, monster_Refresh_config } from '../../manager/AppControll';
import { DataHelper } from '../../system/DataHelper';
import { Move } from '../component/Move';
import { Tools } from '../../tools/Tools';
import { MonsterManager } from './MonsterManager';
import EvtManger from '../../tools/EvtManger';
import { CoinDropType } from '../Drop';
import { LevelMonsterManager } from '../levelModel/LevelMonsterManager';
import { FightControl, GameModel } from '../FightControl';
const { ccclass, property } = _decorator;


@ccclass('Boss')
export class Boss extends MonsterOrigin {
    private MBIsTimeOut = false;
    private TotalTime = 0;
    protected onLoad(): void {
        super.onLoad();
    }

    InitMonster(config: monster_Refresh_config, base: monster_base_config) {
        this.refreshConfig = config;
        this.baseConfig = base;
        this.node.scale = Vec3.ONE;
        this.direction = this.GetDirection();
        this.InitHpComponent();
        this.InitMoveComponent();
        this.InitAnimiton();
    }

    MoveFinish() {
        if (FightControl.gamemodel == GameModel.levelModel) {
            super.MoveFinish();
            if (FightControl.gamemodel == GameModel.levelModel) {
                EvtManger.do(EventName.levelModeMoveFinish, this.baseConfig.baseBlood);
            }
            return;
        }
        if (!this.MBIsTimeOut) {
            let speed = this.baseConfig.baseSpeed * this.refreshConfig.speedRatio;
            this.node.getComponent(Move).Reverse(speed);
        } else {
            this.node.active = false;
            this.node.destroy();
            MonsterManager.ndBoss = null;
            if (FightControl.gamemodel == GameModel.normal) {
                Tools.RemoveElementByVaule(MonsterManager.ltMonster as [], this.node);
            } else if (FightControl.gamemodel == GameModel.levelModel) {
                Tools.RemoveElementByVaule(LevelMonsterManager.ltMonster as [], this.node);
            }
            EvtManger.do(EventName.monsterDeath, this.node);
            EvtManger.do(EventName.bossDeath);
        }
    }

    Die(): void {
        super.Die();
        if (FightControl.gamemodel == GameModel.normal) MonsterManager.ndBoss = null;
        if (FightControl.gamemodel == GameModel.levelModel) LevelMonsterManager.ndBoss = null;
        EvtManger.do(EventName.dropCoins, this.node.position.clone(), CoinDropType.ten);
        EvtManger.do(EventName.bossDeath, this.node.position.clone());
    }

    InitMoveComponent() {
        let id = 0;
        if (FightControl.gamemodel == GameModel.normal) {
            id = Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.bossLine).value);
        } else {
            let ltTemp = this.refreshConfig.lineRoute;
            id = this.refreshConfig.lineRoute[0];
            if (ltTemp.length > 0) {
                id = ltTemp[Math.floor(Math.random() * (ltTemp.length))];
            }
        }
        let speed = this.baseConfig.baseSpeed * this.refreshConfig.speedRatio;
        let lineConfig = DataHelper.GetMoveLineConfigById(id);
        if (FightControl.gamemodel == GameModel.levelModel) {
            this.node.addComponent(Move).InitPointLt(lineConfig.levelLine, speed)
            return;
        }
        let startPoint;
        let endPoint;
        if (this.direction == 1) {
            startPoint = new Vec2(lineConfig.leftPosition[0], lineConfig.leftPosition[1]);
            endPoint = new Vec2(lineConfig.rightPosition[0], lineConfig.rightPosition[1]);
        } else {
            startPoint = new Vec2(lineConfig.rightPosition[0], lineConfig.rightPosition[1]);
            endPoint = new Vec2(lineConfig.leftPosition[0], lineConfig.leftPosition[1]);
        }
        this.startPoint = startPoint;
        this.endPoint = endPoint;

        this.node.addComponent(Move).InitPoint(startPoint, endPoint, speed);
    }

    protected update(dt: number): void {
        super.update(dt);
        if (this.mCurTime >= Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.bossStopTime).value)) {
            this.MBIsTimeOut = true;
        }
    }
}
